package GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;

import conference.Conference;
import conference.ConferenceIO;
import papers.Manuscript;
import users.Reviewer;
import users.User;

/**
 * A class that allow to change the manuscript status.
 * @author Dickson Kwong
 * revision: Matt Minch
 * @version v1.2 Nov. 30, 2011
 */

@SuppressWarnings("serial")
public class PCViewAllUserPanel extends JPanel {

	/**
	 * The box.
	 */
	private Box my_box;
	
	/**
	 * The panel.
	 */
	private PCViewReviewerPanel my_panel;
	
	/**
	 * The conference.
	 */
	private Conference my_conf;
	
	/**
	 * The manuscript location.
	 */
	private int my_manuscript_loc;
	
	/**
	 * The PCViewAllUserPanel Constructor. It creates a panel list all
	 * all the user (reviewer).
	 * @param the_manuscript The manuscript
	 * @param the_panel The panel
	 * @param the_conference The conference
	 */
	public PCViewAllUserPanel(Manuscript the_manuscript, 
			PCViewReviewerPanel the_panel, Conference the_conference) {
	    super(true);
	    setSize(200, 600);
	    my_panel = the_panel;
	    my_conf = the_conference;
	    for (int i = 0; i < my_conf.getManuscripts().size(); i++){
	    	if(the_conference.getManuscripts().get(i) == the_manuscript)
	    		my_manuscript_loc = i;
	    }
	    setup();
	}
	
	/**
	 * It add all the necessary items to the panel.
	 */
	private void setup(){
		List<User> user_list = my_conf.getUsers();
		final Box bv = Box.createVerticalBox();
		for(int i = 0; i < user_list.size(); i++){
			if(!my_conf.getManuscripts().get(my_manuscript_loc).getReviewers().contains(user_list.get(i))){
				if(user_list.get(i) instanceof Reviewer && !user_list.get(i).getName()
						.equals(my_conf.getManuscripts().get(my_manuscript_loc).getAuthor().getName()))
				{
					final User reviewer = user_list.get(i);
					final JButton button = new JButton(reviewer.getName());
					button.setPreferredSize(new Dimension(400, 80));
					button.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							button.setVisible(false);
							my_panel.append(button.getText());
							my_conf.getManuscripts().get(my_manuscript_loc).addReviewer((Reviewer) reviewer);
							((Reviewer) reviewer).addAssignedManuscript(my_conf.getManuscripts().get(my_manuscript_loc));
							ConferenceIO.saveConference(System.getProperty("user.dir")+ File.separator, my_conf);
						}
					});
					bv.add(button);	
				}
			}
	    }
		my_box = bv;
		add(my_box);
	}
}
